package com.example.ymm.modules1.service.impl.sys;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.modules1.entity.my.sys.DbTest;
import com.example.ymm.modules1.mapper.my.sys.DbTestMapper;
import com.example.ymm.modules1.service.dto.DbTestQueryCriteria;
import com.example.ymm.modules1.service.sys.DbTestService;
import com.example.ymm.util.DateKit;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileCopyUtils;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

@Service
@DSTransactional
@Slf4j
public class DbTestServiceImpl extends BaseServiceImpl<DbTestMapper, DbTest> implements DbTestService {


    HashMap<String,String> draw_depart_map=new HashMap<>();;//存储已抽中

    List<String> allMs=new ArrayList<>();

    HashMap<String,String> all_depart=new HashMap<>();
    public List<DbTest> queryAll(DbTestQueryCriteria criteria) {
        List<DbTest> result =baseMapper.selectList(QueryHelpPlus.getPredicate(DbTest.class, criteria));
        return result;
    }

    @PostConstruct
    public void initFn(){
        String s="生产部,品管部,供应商管部/财务部,设计部,企管部,人力行政,工程部,业务部";
        List<String> list = Arrays.asList(s.split(","));
        for(String k : list){
            all_depart.put(k,k);

        }
    }


    @Override
    public List<String> departRandom(String user) {

//        List<String> ms=new ArrayList<>();
        allMs.add("当前抽签人员:"+user+ DateKit.getStringNowTime());

        HashMap<String,String> this_all_depart=new HashMap<>();
        for(String k1:all_depart.keySet()){
            this_all_depart.put(k1,k1);
        }
        //排除已抽
        for(String k2:draw_depart_map.keySet()){
            this_all_depart.remove(k2);
        }
        allMs.add("待抽奖部门"+this_all_depart.keySet().size()+":"+this_all_depart.keySet());

        try{
            ClassPathResource region = new ClassPathResource("region.json");
            InputStream inputStream = region.getInputStream();
            String copy = FileCopyUtils.copyToString(new InputStreamReader(inputStream));
            JSONArray objects = JSON.parseArray(copy);

            Integer user_index=null;
            String user_depet=null;//用户部门
            StringBuffer result_depet=new StringBuffer();//匹配部门结果
            HashMap<String,String> depet_map=new HashMap<>();//部门列表
            //关联部门
            HashMap<String,String> ref_map=new HashMap<>();
            ref_map.put("供应商管部","财务部");
            ref_map.put("财务部","供应商管部");
            for(int a=0;a<objects.size();a++){
                JSONArray childArr = objects.getJSONArray(a);
                for(int b=0;b<childArr.size();b++){
                    JSONObject jsonObject = childArr.getJSONObject(b);
                    String u_d=jsonObject.getString("u");
                    String d=jsonObject.getString("d");
                    if(user.equals(u_d)){
                        user_index=a;
                        user_depet=d;
                        if(d.equals("供应商管部")||d.equals("财务部")){
                            result_depet.append("供应商管部/财务部");
                        }else{
                            result_depet.append(d);
                        }
                    }
                    depet_map.put(d,d);
                }
            }
            if(UtilValidate.isEmpty(user_index)){
                throw new RuntimeException("--员工名称有误！");
            }

            allMs.add("--匹配到抽奖部门当中的哪个:"+result_depet.toString());
            //获取用户下标部门
            HashMap<String,String> user_index_depet_map=new HashMap<>();//用户下标部门列表
            JSONArray user_index_jsonArray = objects.getJSONArray(user_index);
            for (int a=0;a<user_index_jsonArray.size();a++){
                JSONObject jsonObject = user_index_jsonArray.getJSONObject(a);
                String d=jsonObject.getString("d");
                if(d.equals("供应商管部")||d.equals("财务部")){
                    user_index_depet_map.put("供应商管部/财务部","供应商管部/财务部");
                }else{
                    user_index_depet_map.put(d,d);
                }
//                user_index_depet_map.put(d,d);
            }

            allMs.add("--匹配到关联部门并移除:"+user_index_depet_map.keySet());


//            String ref_value = ref_map.get(user_depet);
//            if(UtilValidate.isNotEmpty(ref_value)){
//                depet_map.remove(ref_value);//移除关联部门
//            }
            //开始抽签
            HashMap<String,String> local_draw_map=new HashMap<>();
            List<String> draw_arr=new ArrayList<>();
            this_all_depart.forEach((k,v)->{
                local_draw_map.put(k,k);
            });
            for(String key : user_index_depet_map.keySet()){
                local_draw_map.remove(key);
            }
            for(String key : local_draw_map.keySet()){
                draw_arr.add(key);
            }

            allMs.add("-----部门池"+draw_arr.size()+":"+draw_arr);

            Random random=new Random();
            int index =random.nextInt(draw_arr.size());
            String draw_k = draw_arr.get(index);
            draw_depart_map.put(draw_k,draw_k);//存储已抽
            this_all_depart.remove(draw_k);//移除抽中部门
            allMs.add("---抽中部门:"+draw_k);
            allMs.add("---已抽中部门:"+draw_depart_map.keySet().size()+":"+draw_depart_map.keySet());
            allMs.add("--剩余待抽部门"+this_all_depart.keySet().size()+":"+this_all_depart.keySet());
            allMs.add("------------分割线-------------");
//            allMs.addAll(ms);
        }catch (Exception e){
            allMs.add(e.getMessage());
            log.error(e.getMessage(),e);
        }
        return allMs;
    }

    @Override
    public void departClear() {
        this.draw_depart_map=new HashMap<>();;//存储已抽中
        this.allMs=new ArrayList<>();
    }
}
